import { Args, Mutation, Parent, Query, ResolveProperty, Resolver } from "@nestjs/graphql";
import { Collection } from "src/entities/collection.entity";
import { Comment } from "src/entities/comment.entity";
import { User } from "src/entities/user.entity";
import { CollectionInput, CollectionSortInput, CollectionWhereInput, CommentInput, CommentSortInput, CommentWhereInput, PageInput } from "src/graphql";
import { ArticleService } from "src/services/article/article.service";
import { CommentService } from "src/services/comment/comment.service";
import { UserService } from "src/services/user/user.service";

@Resolver('Comment')
export class CommentResolver {
  constructor(
    private readonly commentService: CommentService,
    private readonly userService: UserService,
    private readonly articleService: ArticleService,
  ) {}

  @Query('comment')
  async getCommentById(@Args() id: number) {
    return await this.commentService.findOne(id);
  }

  @Query('comments')
  async getComments(@Args('page') page: PageInput,@Args('sort') sort:CommentSortInput,@Args('where') where:CommentWhereInput) {
    return await this.commentService.getPagination(page,sort,where);
  }

  @Mutation()
  async addComment(@Args('comment') comment:CommentInput) {
    return await this.commentService.addOne(<Comment><unknown>comment);
  }

  @Mutation()
  async removeComment(@Args() id: number) {
    return await this.commentService.removeOne(id);
  }

  @Mutation()
  async modifyComment(@Args('id') id: number, @Args('comment') comment:CommentInput) {
    return await this.commentService.modifyOne(id,<Comment><unknown>comment);
  }

  @ResolveProperty(() => User,{
    name:'_user'
  })
  async user(@Parent() comment){
    const { user_id } = comment;
    return await this.userService.findOne(user_id);
  }

  @ResolveProperty(() => User,{
    name:'_article'
  })
  async article(@Parent() comment){
    const { article_id } = comment;
    return await this.articleService.findOne(article_id);
  }

  @ResolveProperty(() => Comment,{
    name:'_parent'
  })
  async parent(@Parent() comment){
    const { pid } = comment;
    return pid==0?null:await this.commentService.findOne(pid);
  }

  @ResolveProperty(() => Comment,{
    name:'_childrens'
  })
  async childrens(@Parent() comment){
    const { id } = comment;
    return id==0?null:await this.commentService.findAll({pid:id});
  }

}